package com.yuanluludev.lib_base.net

import android.content.Context
import android.net.ConnectivityManager
import com.google.gson.JsonSyntaxException
import com.yuanluludev.lib_base.application.BaseApplication
import com.yuanluludev.lib_utils.util.ToastUtils
import kotlinx.coroutines.TimeoutCancellationException
import rxhttp.wrapper.exception.HttpStatusCodeException
import rxhttp.wrapper.exception.ParseException
import java.net.ConnectException
import java.net.SocketException
import java.net.SocketTimeoutException
import java.net.UnknownHostException
import java.util.concurrent.TimeoutException

/**
 * author: yuanlulu
 * Date: 2020/07/23
 * des: Base bean
 */
fun Throwable.show() {
    (errorMsg ?: message)?.show()
}

fun Throwable.show(standbyMsg: String) {
    (errorMsg ?: standbyMsg).show()
}

fun Throwable.show(standbyMsg: Int) {
    (errorMsg ?: BaseApplication.getThisApplication().getString(standbyMsg)).show()
}

fun String.show() {
    ToastUtils.showShort(this)
}

val Throwable.errorCode: String
    get() {
        val errorCode = when (this) {
            is HttpStatusCodeException -> {//请求失败异常
                this.statusCode.toString()
            }
            is ParseException -> {  // ParseException异常表明请求成功，但是数据不正确
                this.errorCode
            }
            else -> {
                "-1"
            }
        }
        return errorCode
    }

val Throwable.isNetException: Boolean
    get() {
        return this is UnknownHostException
                || this is SocketTimeoutException
                || this is TimeoutException
                || this is ConnectException
                || this is HttpStatusCodeException
    }

val Throwable.errorMsg: String
    get() {
        return if (this is UnknownHostException) { //网络异常
            if (!isNetworkConnected(BaseApplication.getThisApplication()))
                "当前无网络，请检查你的网络设置"
            else
                "网络连接不可用，请稍后重试！"
        } else if (
            this is SocketTimeoutException  //okhttp全局设置超时
            || this is TimeoutException     //rxjava中的timeout方法超时
            || this is TimeoutCancellationException  //协程超时
        ) {
            "连接超时,请稍后再试"
        } else if (this is SocketException) { //下载的时候  中断网络  会走SocketException
            "连接失败，请稍候重试！"
        } else if (this is ConnectException) {
            "网络不给力，请稍候重试！"
        } else if (this is HttpStatusCodeException) { //请求失败异常
            "服务正在升级，请稍后再试"
        } else if (this is JsonSyntaxException) {  //请求成功，但Json语法异常,导致解析失败
            "数据解析失败,请检查数据是否正确"
        } else if (this is ParseException) {       // ParseException异常表明请求成功，但是数据不正确
            this.message ?: errorCode   //msg为空，显示code
        } else {
            message ?: this.toString()
        }
    }

val Throwable.requestMethod: String?
    get() {
        //说明是能获取到数据 但是code非零
        return if (this is ParseException) this.requestMethod else ""
    }

private fun isNetworkConnected(context: Context): Boolean {
    val mConnectivityManager =
        context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
    val mNetworkInfo = mConnectivityManager.activeNetworkInfo
    if (mNetworkInfo != null) {
        return mNetworkInfo.isAvailable
    }
    return false
}
